package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.util.ToastUtil;

//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.OnClick;

public class GuoxueBuyVIPActivity extends BaseActivity implements View.OnClickListener {

    TextView guoxueNickname;
    ImageView guoxueKaitong;
    ImageView myzhanghu;
    ImageView arrowZhanghu;
    RelativeLayout guoxueShuomingLayout;
    ImageView myzhanghuIv;
    ImageView arrowZhanghuIv;
    RelativeLayout guoxueShouyiLayout;
    LinearLayout guoxueHuiyuan;
    private PopupWindow payPopwindow;
    private RelativeLayout linearLayout;
    private LinearLayout yonghuxieyi;
    private ImageView close;
    private TextView jibie;
    private TextView jianjie, vip_end_time;
    private TextView pay_num;
    private TextView pay_btn, back, shouyi;
    private EditText yaoqingma;
    private String jump_jibie;
    private boolean agree_dis = true;
    private ImageView pay_close;
    private int detailTaskId = -1;
    private EatShowUserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guoxue_buy_vip);
        initUI();
        getUserDetail();
    }

    private void initUI() {
        guoxueNickname = (TextView) findViewById(R.id.guoxue_nickname);
        guoxueKaitong = (ImageView) findViewById(R.id.guoxue_kaitong);
        vip_end_time = (TextView) findViewById(R.id.vip_end_time);
        shouyi = (TextView) findViewById(R.id.shouyivip);
        guoxueKaitong.setOnClickListener(this);
        back = (TextView) findViewById(R.id.zhuanyun_back);
        back.setOnClickListener(this);
        guoxueShuomingLayout = (RelativeLayout) findViewById(R.id.guoxue_shuoming_layout);
        guoxueShuomingLayout.setOnClickListener(this);
        guoxueShouyiLayout = (RelativeLayout) findViewById(R.id.guoxue_shouyi_layout);
        guoxueHuiyuan = (LinearLayout) findViewById(R.id.guoxue_huiyuan);
        Intent intent = getIntent();
        if (intent.getStringExtra("nickname") != null) {
            String nickname = intent.getStringExtra("nickname");
            guoxueNickname.setText(nickname);
        }
    }

    private void getUserDetail() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new MyListener(mContext));
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getPopwindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.pay_popwindow, null);
        linearLayout = (RelativeLayout) view.findViewById(R.id.pay_bg_ll);
        yonghuxieyi = (LinearLayout) view.findViewById(R.id.yonghuxieyi_ll);
        yonghuxieyi.setOnClickListener(this);
        pay_close = (ImageView) view.findViewById(R.id.zhuanyun_close);
        pay_close.setOnClickListener(this);
        jibie = (TextView) view.findViewById(R.id.zhuanyun_jibie);
        jianjie = (TextView) view.findViewById(R.id.zhuanyun_jianjie);
        pay_num = (TextView) view.findViewById(R.id.zhuanyun_pay_num);
        pay_btn = (TextView) view.findViewById(R.id.zhuanyun_pay_now);
        pay_btn.setOnClickListener(this);
        yaoqingma = (EditText) view.findViewById(R.id.yaoqingma);
        linearLayout.setBackgroundResource(R.drawable.my_vipbgx);
        jibie.setText("国学会员");
        jianjie.setText("优质视频、漫画享不停");
        pay_num.setText("年费：￥2998");
//        WindowManager wm = (WindowManager) getContext()
//                .getSystemService(Context.WINDOW_SERVICE);
//
//        int width = wm.getDefaultDisplay().getWidth();
//        int height = wm.getDefaultDisplay().getHeight();
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        payPopwindow = new PopupWindow(view, width - 40, height / 2, true);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().setAttributes(lp);
//        ColorDrawable dw = new ColorDrawable(0x60000000);
        payPopwindow.setBackgroundDrawable(new ColorDrawable());
        payPopwindow.setFocusable(true);
        payPopwindow.setOutsideTouchable(true);
        payPopwindow.showAtLocation(guoxueHuiyuan, Gravity.CENTER, 0, 0);
    }

    class MyListener extends ShowToastTaskEventListener {

        private String s;

        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);

            if (detailTaskId == taskId)
                if (data != null) {
                    userInfo = (EatShowUserInfo) data;
                    if (userInfo != null) {
                        if (userInfo.getIsVip2().equals("1")) {
                            guoxueKaitong.setImageResource(R.drawable.guoxue_btn_x);
                        }
                        if (!TextUtils.isEmpty(userInfo.getVipEndDate2())) {
                            vip_end_time.setText("您的会员到期时间是" + userInfo.getVipEndDate2());
                        } else {
                            vip_end_time.setText("");
                        }
                        if (userInfo.getVipFlower() != null) {
                            shouyi.setText(userInfo.getVipFlower() + "学分");
                        }

                    }
                }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yonghuxieyi_ll:

                break;
            case R.id.zhuanyun_back:
                finish();
                break;
            case R.id.guoxue_kaitong:
                getPopwindow();
                break;
            case R.id.guoxue_shuoming_layout:
                Intent intent = new Intent(mContext, HuiyuanShuomingActivity.class);
                startActivity(intent);
                break;
            case R.id.guoxue_shouyi_layout:
                break;
            case R.id.zhuanyun_close:
                payPopwindow.dismiss();
                break;
            case R.id.zhuanyun_pay_now:
                if (agree_dis) {
                    String s = yaoqingma.getText().toString();
                    intent = new Intent(this, RechargeActivity.class);
                    intent.putExtra("type", "guoxuexueyuan");
                    intent.putExtra("yaoqingma", s);
                    intent.putExtra("money", "2998");
                    startActivity(intent);
                } else {
                    ToastUtil.showMessage(this, "请先同意用户购买协议");
                    return;
                }
                break;
        }
    }
}
